<template>
  <div>
    <!-- 顶部搜索框 -->
    <van-nav-bar
      fixed
      :border="false"
      left-arrow
      right-text="搜索"
      @click-right="onSearch"
      @click-left="onClickLeft"
    >
      <template #title>
        <van-search
          v-model="value"
          :left-icon="require('../assets/baking_log/search.png')"
          icon-size="12"
          placeholder="搜索食谱或食材"
          @search="onSearch"
        >
        </van-search>
      </template>
    </van-nav-bar>
    <div style="height: 46px"></div>
    <van-tabs sticky offset-top="46px" v-model="active" line-width="50">
      <van-tab title="综合排序">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <recipe-cell v-for="i in 10" :key="i"></recipe-cell>
        </van-list>
      </van-tab>
      <van-tab title="做过最多">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <recipe-cell v-for="i in 10" :key="i"></recipe-cell>
        </van-list>
      </van-tab>
      <van-tab title="达人食谱">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <recipe-cell v-for="i in 10" :key="i"></recipe-cell>
        </van-list>
      </van-tab>
    </van-tabs>
  </div>
</template>

<script>
import RecipeCell from "@/components/RecipeCell.vue";
export default {
  components: {
    RecipeCell,
  },
  data() {
    return {
      value: "",
      historyList: [], //历史搜索记录，存本地
      active: 0,
      loading: false,
      finished: false,
      // data: null,
    };
  },
  mounted() {
    if (JSON.parse(localStorage.getItem("historyList"))) {
      this.historyList = JSON.parse(localStorage.getItem("historyList"));
    }
  },
  methods: {
    onLoad() {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      // 加载状态结束
      // this.loading = false;
      // // 数据全部加载完成
      // if (this.data.length >= 40) {
      //   this.finished = true;
      // }
    },
    onClickLeft() {
      return this.$router.go(-1);
    },
    onSearch() {
      // 如果为空，则弹出提示信息
      if (this.value.trim() == "") {
        this.$toast("请输入搜索内容");
        return 0;
      } else {
        // 没有搜索记录，把搜索值push进数组首位，存入本地
        if (!this.historyList.includes(this.value)) {
          this.historyList.unshift(this.value);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        } else {
          // 有搜索记录，删除之前的旧纪录，将新搜索值重新push到数组首位
          let i = this.historyList.indexOf(this.value);
          this.historyList.splice(i, 1);
          this.historyList.unshift(this.value);
          localStorage.setItem("historyList", JSON.stringify(this.historyList));
        }
        // 跳转到搜索结果页
        if (this.$route.path == `/recipe_list?rname=${this.value}`) return;
        this.$router.push({
          path: `/recipe_list`,
          query: {
            rname: this.value,
          },
        });
      }
    },
  },
};
</script>

<style scoped>
:deep(.van-nav-bar__text) {
  color: #333;
}
:deep(.van-nav-bar .van-icon) {
  color: #333;
}
:deep(.van-nav-bar) {
  overflow: hidden;
}
:deep(.van-nav-bar__title) {
  max-width: 90%;
  padding-right: 10px;
}
:deep(.van-search) {
  width: 80vw;
}
:deep(.van-icon__image) {
  margin-top: 4px;
}
:deep(.van-row) {
  margin-top: 20px;
  margin-bottom: 5px;
}
:deep(.van-col) {
  color: #a2a2a2;
  font-size: 15px;
}
:deep(.van-tabs__line) {
  background-color: #ea9077;
}
:deep(.van-tabs__nav--line) {
  padding-bottom: 8px;
}
</style>

<style lang="scss" scoped></style>
